package com.mobware4u.random;

import java.util.ArrayList;
import java.util.Random;

import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class Randomization extends Activity {

    Panel mView = null;

    // ends the thread
    volatile boolean isRunning = true;

    // greater this value , slower the plot
    int SPEED = 100;

    // the bigger this value, the larger the dot
    int POINT_SIZE = 5;

    // hold the random co ordinates generated
    ArrayList<Coord> mCoords = new ArrayList<Coord>();

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // create and display a surface view
        mView = new Panel(this);
        setContentView(mView);
    }

    @Override
    public void onPause() {
        super.onPause();
        isRunning = false;
    }

    /**
     * The coordinate class for a point
     * 
     * @author hashir
     * 
     */
    class Coord {
        public int x;
        public int y;

        public Coord(int x, int y) {
            this.x = x;
            this.y = y;
        }
    }

    /**
     * The surface view
     * 
     * @author hashir
     * 
     */
    class Panel extends SurfaceView implements SurfaceHolder.Callback {
        private Paint paint;

        public Panel(Context context) {
            super(context);

            paint = new Paint();
            paint.setColor(Color.YELLOW);

            getHolder().addCallback(this);
            setFocusable(true);
        }

        public void surfaceChanged(SurfaceHolder holder, int format, int width,
                int height) {
            // TODO
        }

        public void surfaceCreated(SurfaceHolder holder) {
            // start the repeater
            new Next().start();
        }

        public void surfaceDestroyed(SurfaceHolder holder) {
        }

        public void paint(Canvas canvas) {

            // generate and store random points
            int randomX = new Random().nextInt(canvas.getWidth());
            int randomY = new Random().nextInt(canvas.getHeight());
            mCoords.add(new Coord(randomX, randomY));

            // display all points
            for (Coord c : mCoords) {
                canvas.drawCircle(c.x, c.y, POINT_SIZE, paint);
            }
        }

    }

    /**
     * Repeat drawing points
     * 
     * @author hashir
     * 
     */
    class Next extends Thread {
        public void run() {

            while (isRunning) {

                runOnUiThread(new Runnable() {

                    @Override
                    public void run() {
                        Canvas c = null;
                        c = null;
                        try {
                            c = mView.getHolder().lockCanvas(null);
                            synchronized (mView.getHolder()) {
                                mView.paint(c);
                            }
                        } finally {
                            if (c != null) {
                                mView.getHolder().unlockCanvasAndPost(c);
                            }
                        }
                    }
                });

                try {
                    Thread.sleep(SPEED);
                } catch (Exception e) {
                }
            }
        }
    }

    
    //MENU ITEM
    public boolean onCreateOptionsMenu(Menu menu) {

        super.onCreateOptionsMenu(menu);
        menu.add(0, 2, 0, "Exit").setIcon(
                android.R.drawable.ic_menu_close_clear_cancel);

        return true;
    }

    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {

        case 2:
            super.onDestroy();
            this.finish();
            break;

        }
        return true;
    }
}